#!/bin/bash
#
# Virtuoso			Graph Delete Script for Virtuoso OSE
#
# Author: Shangguan (http://www.cs.rpi.edu/~shangz, shangz@cs.rpi.edu)
# Version: 1.0
# Date: Dec 12, 2010
# Description: Bash script to delte a named graph in Virtuoso
# Usage: vdelete [graph_uri]

# Get input arguments
args=("$@")
LOGFILE="/tmp/vad/vdelete-`date +%Y%m%d-%H%M%S`.log"

if [ $# -ne 1 ]; then
    echo "Wrong number of arguments. Correct usage: \"vdelete [graph_uri]\""
else
    # Get graph_uri
    graph_uri=${args[0]}

    # Status message
    echo "Deleting triples from graph <$graph_uri>, please wait..."

    # Log into Virtuoso isql env
    isql_cmd="/opt/virtuoso-6.1.4/bin/isql 1111 dba"
    isql_pwd="dba"  # default admin password, may need to change

    # Call SPARQL CLEAR to delete named graphs
    sparql_delete="SPARQL CLEAR GRAPH <$graph_uri>;"
    ${isql_cmd} ${isql_pwd} << EOF &> ${LOGFILE}
	$sparql_delete
	checkpoint;
	exit;
EOF
    result=$?
    if [ $result != 0 ]
    then
	echo "Failed to delete the graph $GRAPH. Check ${LOGFILE} for details"
	exit $result
    fi

    # Status message
    echo "Deleting finished! Check ${LOGFILE} for details."
fi

